package com.example.demo;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class SelectDivice extends Activity {
	private  GridView selectDivice;
	protected Myapplication app;
    private Socket sc;
    OutputStreamWriter send;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_divice);
        selectDivice=(GridView) findViewById(R.id.selectDivice);
        selectDivice.setAdapter(new ImageAdapter(this));
        app=(Myapplication)getApplication();
        sc=app.getSocket();
        
        try {
			 send= new OutputStreamWriter(sc.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        selectDivice.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				switch(position){
				case 0://light device
					//String ipString  =IP.getText().toString();
					//String portString=Port.getText().toString();
					//Bundle sendBundle = new Bundle();
					//sendBundle.putString("ipString", ipString);
					//sendBundle.putString("portString",portString);
					try {
						send.write("chon bong den");
						send.flush();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					Intent i = new Intent(SelectDivice.this, LightDivece.class);
					//i.putExtras(sendBundle);
					startActivity(i);
					break;
				case 1:
					break;
				case 2:
					break;
				case 3:
					break;
				case 4:
					break;
				
			}
			}
		});
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_select_divice, menu);
        return true;
    }
}
